package com.chinasoft.goodsManager.dao.impl;

import com.chinasoft.goodsManager.dao.GoosListDao;
import com.chinasoft.goodsManager.daomaingoodslist.Goods;
import com.chinasoft.goodsManager.daomaingoodslist.UserList;
import com.chinasoft.goodsManager.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class GoodsListDaoImpl implements GoosListDao {
    static JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    //查询数据库返回数据
    @Override
    public List<Goods> query() {
        try {
            String sql = "select * from  g_goods";
            List<Goods> query = template.query(sql, new BeanPropertyRowMapper<>(Goods.class));
            return query;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }

    }

    //删除行
    @Override
    public void goodsselete(Integer id) {
        String sql = "delete from g_goods where id=?";
        template.update(sql,id);
    }

    //userlist页面查询
    @Override
    public List<UserList> querys() {
        try {
            String sql = "select * from  g_preson";
            List<UserList> query = template.query(sql, new BeanPropertyRowMapper<>(UserList.class));
            return query;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    //删除行
    @Override
    public void userSelete(Integer id) {
        String sql = "delete from g_preson where id=?";
        template.update(sql,id);
    }





    //读取文本
    @Override
    public Object [] detailed( Integer id) {
        try {
            int i = 0;
            Object [] arr = new Object[6];
            String sql = "select idcard,qq,pemail,pimg,pdescrike from  g_preson where id=?";
            List<Map<String, Object>> maps = template.queryForList(sql,id);
            for (Map<String, Object> map : maps) {
                Set<String> key = map.keySet();
                for (String s : key) {
                    arr[i] = map.get(s);
                    i++;
                }
            }

            FileReader txt = new FileReader("D:\\GoodManagerFile\\PersonFile\\"+arr[4]);
            int len;
            String str="";
            while ((len = txt.read()) != -1){
                 str = str + (char)len;
            }
            txt.close();
            arr[5] = str;
            return arr;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void add(UserList userList) {
        String sql= "insert into g_preson values (?,?,?,?,?,?,?,?,?,?,?,?) ";
        template.update(sql,null,userList.getPimg(),userList.getPname(),userList.getIdcard(),userList.getJob(),userList.getPsalary(),userList.getWyear(),userList.getMphone(),userList.getQq(),userList.getPemail(),userList.getPaddress(),userList.getPdescrike());
    }

    @Override
    public List<Map<String, Object>> modift(Integer integer) {
        String sql = "select * from g_preson  where id=?";
        List<Map<String, Object>> maps = template.queryForList(sql, integer);
        return maps;
    }

    @Override
    public void update(UserList userList) {
        String sql = "update g_preson set pimg=?,pname=?,idcard=?,job=?,psalary=?,wyear=?,mphone=?,qq=?,pemail=?,paddress=?,pdescrike=? where id=?";
        template.update(sql,userList.getPimg(),userList.getPname(),userList.getIdcard(),userList.getJob(),userList.getPsalary(),userList.getWyear(),userList.getMphone(),userList.getQq(),userList.getPemail(),userList.getPaddress(),userList.getPdescrike(),userList.getId());
    }

    @Override
    public Object[] person(Integer id) {
        String sql = "select gimg,gdescrike from g_goods  where id=?";
        String arr[] = new String[2];
        int i = 0;
        List<Map<String, Object>> maps = template.queryForList(sql, id);
        for (Map<String, Object> map : maps) {
            Set<String> key = map.keySet();
            for (String s : key) {
                arr[i]= String.valueOf(map.get(s));
                i++;
            }
        }

        try {
            FileReader txt = new FileReader("D:\\GoodManagerFile\\PersonFile\\"+arr[1]);
            int len;
            String str="";
            while ((len = txt.read()) != -1){
                str = str + (char)len;
            }
            txt.close();
            arr[1] = str;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return arr;
    }


}
